package com.wzz.controller;

import com.alibaba.fastjson.JSON;
import com.rabbitmq.client.Channel;

import com.wzz.config.FreemarkerUtil;
import com.wzz.pojo.Product;
import com.wzz.pojo.vo.PageInfoVo;
import com.wzz.pojo.vo.R;
import com.wzz.service.ProductService;
import freemarker.template.TemplateException;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.UUID;

/**
 * @author DarkClown
 * @date 2024-08-02 16:12
 * @project_name week3-try
 * @parem
 **/
@RestController
@RequestMapping("/list/list")
public class ListController {
    @Autowired
    ProductService productService;

    @Autowired
    RabbitTemplate rabbitTemplate;

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @PostConstruct
    public void innit(){
        rabbitTemplate.setReturnCallback(
                new RabbitTemplate.ReturnCallback() {
                    @Override
                    public void returnedMessage(Message message, int replyCode, String replyText, String exchange, String routingKey) {
                        rabbitTemplate.convertAndSend(routingKey,exchange,message);
                    }
                }
        );
        rabbitTemplate.setConfirmCallback(
                new RabbitTemplate.ConfirmCallback() {
                    @Override
                    public void confirm(CorrelationData correlationData, boolean ack, String cause) {
                        if (ack){
                            System.out.println("发送成功");
                        }else {
                            System.out.println("发送失败");
                        }
                    }
                }
        );
    }


    /**
     * 完成列表的分页,匹配,模糊查询,日期查询,排序功能
     * @param pageInfoVo
     * @return
     */
    @RequestMapping("selectList")
    private R selectList(@RequestBody PageInfoVo pageInfoVo){
        return productService.selectList(pageInfoVo);
    }


    @RequestMapping("formatList")
    private R formatList(){
        return productService.selectFormatList();
    }

    @RequestMapping("addProduct")
    private R addOrUpdateProduct(@RequestBody Product product){
        //设置创建时间   创建
        if (product.getPid()==null) {
            product.setCreateTime(new Date());
            String code = "";
            if (product.getFormat().equals("前缀+字母和数字")) {
                String codeStr = RandomStringUtils.randomAlphabetic(2);
                int codeInt = RandomUtils.nextInt(999, 9999);
                code ="ps"+ codeStr + codeInt;
            }

            if (product.getFormat().equals("前缀+字母（不限大小写）")){
                String codeStr = RandomStringUtils.randomAlphanumeric(5);
                code = "ps"+codeStr;
            }
            if (product.getFormat().equals("前缀+数字")){
                int codeInt = RandomUtils.nextInt(999, 9999);
                code = "ps"+codeInt;
            }
            if (product.getFormat().equals("前缀+字母（大写）")) {
                String codeStr = RandomStringUtils.randomAlphabetic(2).toUpperCase();
                int codeInt = RandomUtils.nextInt(999, 9999);
                code ="ps"+ codeStr + codeInt;
            }
            product.setSecurity(code);
            String s = UUID.randomUUID().toString();
//            CorrelationData uuid = new CorrelationData(s);
            rabbitTemplate.convertAndSend("addExchange","", JSON.toJSONString(product));

//            stringRedisTemplate.


        }
        //设置修改时间   修改
        if (product.getPid()!=null){
            product.setUpdateTime(new Date());
        }

        productService.saveOrUpdate(product);

        return R.OK();
    }


    @RabbitListener(queues = "addQueue1")
    public void Customer(Message message, Channel channel){
        String s = new String(message.getBody());

        FreemarkerUtil freemarkerUtil = new FreemarkerUtil();

        HashMap<String, Object> map = new HashMap<>();

        map.put("num",1);

        String outputPath = "C:\\Users\\Administrator\\Desktop\\Freemaker\\" + UUID.randomUUID().toString() + ".html";

        try {
            freemarkerUtil.createHtml(map,"template",outputPath);

            channel.basicAck(message.getMessageProperties().getDeliveryTag(),true);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (TemplateException e) {
            throw new RuntimeException(e);
        }

    }
    @RabbitListener(queues = "addQueue2")
    public void Customer2(Message message, Channel channel){
        String s = new String(message.getBody());

        FreemarkerUtil freemarkerUtil = new FreemarkerUtil();

        HashMap<String, Object> map = new HashMap<>();

        map.put("num",1);

        String outputPath = "C:\\Users\\Administrator\\Desktop\\Freemaker\\" + UUID.randomUUID().toString() + ".html";

        try {
            freemarkerUtil.createHtml(map,"template",outputPath);

            channel.basicAck(message.getMessageProperties().getDeliveryTag(),true);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (TemplateException e) {
            throw new RuntimeException(e);
        }

    }

}
